package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.EmploymentListResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.RichTextResult;
import com.eqdd.yiqidian.ui.select.SelectSingleStaffActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;

/**
 * @author吕志豪 .
 * @date 17-11-24  下午1:07.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class ReturnCarActivity extends SlimBaseMultiEditActivity {

    private RecyclerViewCustom dataBinding;
    private int id;
    private String msg;
    private EmploymentListResultBean employmentListResultBean;
    private RichTextResult richTextResult;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "归还用车");
        initTopRightText("确定", v -> {
            if (checkParams()) {
                PostRequest<HttpResult> params = OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.RETURN_CAR)
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("comid", AppConstent.user.getCompanyId())
                        .params("vehicleId", id)
                        .params("givebackTime", setParam(0))
                        .params("startingMileage", setParam(1))
                        .params("endingMileage", setParam(2))
                        .params("fuelBills", setParam(3))
                        .params("roadToll", setParam(4))
                        .params("sendee", setParam(5))
                        .params("remark", richTextResult == null ? " " : richTextResult.getContent());
                if (richTextResult != null) {
                    for (int i = 0; i < richTextResult.getSelectMedia().size(); i++) {
                        params.params("file" + i, new File(richTextResult.getSelectMedia().get(i).getCompressPath()));
                    }
                }
                params.execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    clearData();
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 5:
                startActivityForResult(SelectSingleStaffActivity.class, null, Config.SELECT_SINGLE_STAFF);
                break;
        }
    }

    @Override
    protected void setEditBeans() {

        id = getIntent().getIntExtra(Config.ID, 0);
        datas.add(new SlimEditTimeBean("*归还时间", "", 0).setType(Config.YEAR_MONTH_DAY_HOUR_MINITE_TYPE).setNeed(true));
        datas.add(new SlimEditEnterBean("*起始里程数(km)", "", 1, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true));
        datas.add(new SlimEditEnterBean("*结束里程数(km)", "", 2, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true));
        datas.add(new SlimEditEnterBean("*油费(元)", "", 3, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true));
        datas.add(new SlimEditEnterBean("*路费(元)", "", 4, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true));
        datas.add(new SlimEditEnterBean("*接收人", "", 5, true, "请选择").setNeed(true));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Config.SELECT_SINGLE_STAFF && resultCode == Config.INTENT_CONTENT) {
            employmentListResultBean = (EmploymentListResultBean) data.getSerializableExtra(Config.BEAN);
            slimAdapterEx.getDataItem(5).setContent(employmentListResultBean.getStaffName());
            slimAdapterEx.notifyItemChanged(5);

        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
